package com.ben.multiple.elasticsearch.entity;

import com.ben.multiple.elasticsearch.resource.LocalDateTimePropertyValueConverter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.elasticsearch.annotations.ValueConverter;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Ben
 */
@Setting(shards = 3, replicas = 1)
@Document(indexName = "person")
@Data
public class Person implements Serializable, Persistable<Long> {
    private static final long serialVersionUID = 1L;

    @Id
    @Field(name = "id", type = FieldType.Long, store = true)
    private Long id;

    @ValueConverter(value = LocalDateTimePropertyValueConverter.class)
    @Field(name = "registerTime", type = FieldType.Date, store = true, pattern = {"yyyy-MM-dd HH:mm:ss"})
    private LocalDateTime registerTime;

    @Field(name = "name", type = FieldType.Text, store = true, analyzer = "ik_max_word")
    private String name;

    @Field(name = "age", type = FieldType.Integer, store = true)
    private Integer age;

    @ValueConverter(value = LocalDateTimePropertyValueConverter.class)
    @Field(name = "birthday", type = FieldType.Date, store = true, pattern = {"yyyy-MM-dd"})
    private LocalDate birthday;

    @GeoPointField
    private GeoPoint birthLocation;

    @Field(name = "birthAddress", type = FieldType.Text, store = true, analyzer = "ik_max_word")
    private String birthAddress;

    @Field(name = "idCardNo", type = FieldType.Keyword, store = true)
    private String idCardNo;

    @Field(name = "height", type = FieldType.Half_Float, store = true)
    private BigDecimal height;

    @Field(name = "weight", type = FieldType.Half_Float, store = true)
    private BigDecimal weight;

    @Field(name = "education", type = FieldType.Keyword, store = true)
    private String education;

    @Field(name = "major", type = FieldType.Text, store = true, analyzer = "ik_max_word")
    private String major;

    @Field(name = "school", type = FieldType.Text, store = true, analyzer = "ik_max_word")
    private String school;

    @Field(name = "hobbit", type = FieldType.Keyword, store = true)
    private List<String> hobbit;

    @Field(name = "status", type = FieldType.Short, store = true)
    private Short status;

    @Field(name = "remark", type = FieldType.Text, store = true, analyzer = "ik_max_word")
    private String remark;

    @JsonIgnore
    @Override
    public boolean isNew() {
        return getId() == null;
    }

}
